package zhoug.demo.common;

import android.Manifest;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import zhoug.common.permission.ZPermissions;
import zhoug.common.receiver.PhoneStateReceiver;
import zhoug.common.utils.PhoneUtils;
import zhoug.common.utils.TimeUtils;
import zhoug.logging.Logger;

/**
 * 拨打电话
 *
 * @Author: zhoug
 * @Date: 2025-02-06
 * @Description:
 */
public class CallPhoneActivity extends MyBaseActivity {
    private static final String TAG = ">>>CallPhoneActivity";
    private String mLog = "";
    private PhoneStateReceiver mPhoneStateReceiver;
    private EditText mEditPhone;
    private Button mBtnCallPhone;
    private TextView mBtnLog;
    private Button mBtnClearLog;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_call_phone;
    }

    @Override
    protected void findViews() {
        mEditPhone = findViewById(R.id.edit_phone);
        mBtnCallPhone = findViewById(R.id.btn_callPhone);
        mBtnLog = findViewById(R.id.btn_log);
        mBtnClearLog = findViewById(R.id.btn_clearLog);
    }


    @Override
    protected void onCreate1(Bundle savedInstanceState) {
        mBtnClearLog.setOnClickListener(v -> {
            clearLog();
        });
        mBtnCallPhone.setOnClickListener(v -> {
            String phone = mEditPhone.getText().toString().trim();
            boolean isPhoneCode = PhoneUtils.isPhoneCode(phone);
            if (!isPhoneCode) {
                toast("电话号码错误");
                return;
            }
            ZPermissions.with(this)
                    .setPermissions(Manifest.permission.CALL_PHONE,Manifest.permission.READ_PHONE_STATE)
                    .setCallback((success, granted, denied) -> {
                        if (success) {
                            PhoneUtils.callPhone(getActivity(), phone);
                        }
                    })
                    .request();
        });

        getLifecycle().addObserver(new DefaultLifecycleObserver() {

            @Override
            public void onCreate(@NonNull LifecycleOwner owner) {
                Logger.d(TAG, "注册电话广播");
                IntentFilter filter = new IntentFilter(PhoneStateReceiver.ACTION_PHONE_STATE_CHANGED);
                mPhoneStateReceiver = new PhoneStateReceiver(mCallback);
                registerReceiver(mPhoneStateReceiver, filter);
            }


            @Override
            public void onDestroy(@NonNull LifecycleOwner owner) {
                Logger.d(TAG, "取消注册电话广播");
                unregisterReceiver(mPhoneStateReceiver);
                mPhoneStateReceiver=null;
            }
        });

    }

    private final PhoneStateReceiver.Callback mCallback=new PhoneStateReceiver.Callback() {
        @Override
        public void onPhoneStateChange(int status) {
            if (status==PhoneStateReceiver.Callback.RINGING) {
                // 正在响铃,来电时才有效
                Logger.d(TAG, "正在响铃");
                mLog += getCurrentTime() + ": 正在响铃" + "\n";
            } else if (status==PhoneStateReceiver.Callback.STATE_OFFHOOK) {
                // 来电电话已接通，去电响铃,可以开始通话处理逻辑（例如更新UI）
                Logger.d(TAG, "电话已接通");
                mLog += getCurrentTime() + ": 电话已接通" + "\n";
            } else if (status==PhoneStateReceiver.Callback.STATE_IDLE) {
                // 电话已挂断，可以处理挂断后的逻辑（例如更新UI）
                Logger.d(TAG, "电话已挂断");
                mLog += getCurrentTime() + ": 电话已挂断" + "\n";
            }
            mBtnLog.setText(mLog);
        }
    };



    private String getCurrentTime() {
        return TimeUtils.getCurrentTime("HH:mm:ss");
    }

    private void clearLog(){
        mLog = "";
        mBtnLog.setText("");
    }



}
